package com.xbongbong.workorder.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.pojo.SummaryDataPoJo;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.parent.interfaces.BusinessSummaryService;
import com.xbongbong.saas.enums.business.WarehouseEnum;
import com.xbongbong.saas.enums.business.WorkOrderEnum;
import com.xbongbong.util.SummaryUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.xbongbong.saas.toolbox.help.FastJsonHelper.getDoubleOrDefaultFromFormData;

/**
 * @author 吴峰
 * @date 2022/02/15 9:31
 */
public class WorkOrderSummaryServiceImpl implements BusinessSummaryService {
    @Override
    public List<PaasFormDataEntityExt> handleSummarySpecialAttr(String corpid, List<SummaryDataPoJo> labelsList, Integer businessType, JSONObject data, Long formDataId, PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException {
        SummaryDataPoJo workOrderSummary = SummaryUtil.getSummaryDataPoJoByAttr(labelsList, WorkOrderEnum.ADDRESS.getAttr());
        if (Objects.nonNull(workOrderSummary)) {
            List<Object> values = workOrderSummary.getValue();
            if (Objects.nonNull(values) && !values.isEmpty()) {
                workOrderSummary.setValue(values);
            }
            Double latitude = getDoubleOrDefaultFromFormData(data, WarehouseEnum.LATITUDE.getAttr(), 0D);
            Double longitude = getDoubleOrDefaultFromFormData(data, WarehouseEnum.LONGITUDE.getAttr(), 0D);
            workOrderSummary.setAddressExtra(latitude, longitude);
        }
        return new ArrayList<>();
    }
}
